package com.sxhuayuan.parking.entity;

import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.ColumnTransformer;

import com.alibaba.druid.util.StringUtils;
import com.sxhuayuan.parking.config.Constant;
import com.sxhuayuan.parking.config.Platform;

/**
 * oem参数配置，配置给某个代理，他的所有子代理必须继承这个配置。
 * 
 * 如果子代理不想走oem通道，他可以选择自己oem或者走平台默认通道。换句话说oem的代理必须是顶级代理，且名下所有子代理和商户必然走此oem通道。
 * 
 * 平台默认通道也是个oem通道
 */
@Entity
@Table(name = "hypk_oem_config")
public class OemConfig extends BaseEntity {

	private static final long serialVersionUID = 5724617875040855036L;

	public static final String SESSION_ATTR_NAME = "session_oem_config";

	/** 微信公众号appid */
	private String wxAppid;

	/** 微信公众号密钥 */
	private String wxSecret;

	/** 子商户公众号appid */
	private String wxSubAppid;

	/** 子商户公众号密钥 */
	private String wxSubSecret;

	/** 微信服务商商户号 */
	private String wxMchid;

	/** 微信子商户号 */
	private String wxSubMchid;

	/** 支付密钥 */
	private String wxMchKey;

	private String alipayAppid;

	private String alipayPriKey;

	private String alipayPubKey;

	/** 公司或机构全称 */
	private String fullName;

	/** 简称 */
	private String forShort;
	/** 大logo 长方形的png log,文字最好为白色 */
	private String maxLogo;

	/** 小logo 圆形图片 */
	private String minLogo;

	/** 网站站标 .ico格式 */
	private String icon;

	/** 此oem配置归属哪个代理，只有此代理才有权修改oem配置 */
	private Long ownerId;

	@NotNull
	@Column(length = 32)
	public String getWxAppid() {
		return wxAppid;
	}

	public void setWxAppid(String wxAppid) {
		this.wxAppid = wxAppid;
	}

	@Column(columnDefinition = "BLOB")
	@ColumnTransformer(read = "CAST(AES_DECRYPT(wx_secret, '" + Constant.DATABASE_AES_SECRET + "') as char(64))", write = "AES_ENCRYPT(?, '" + Constant.DATABASE_AES_SECRET + "')")
	public String getWxSecret() {
		return wxSecret;
	}

	public void setWxSecret(String wxSecret) {
		this.wxSecret = wxSecret;
	}

	@NotNull
	@Column(length = 32)
	public String getWxSubAppid() {
		return wxSubAppid;
	}

	public void setWxSubAppid(String wxSubAppid) {
		this.wxSubAppid = wxSubAppid;
	}

	@Column(columnDefinition = "BLOB")
	@ColumnTransformer(read = "CAST(AES_DECRYPT(wx_sub_secret, '" + Constant.DATABASE_AES_SECRET + "') as char(64))", write = "AES_ENCRYPT(?, '" + Constant.DATABASE_AES_SECRET + "')")
	public String getWxSubSecret() {
		return wxSubSecret;
	}

	public void setWxSubSecret(String wxSubSecret) {
		this.wxSubSecret = wxSubSecret;
	}

	@Column(length = 32)
	public String getAlipayAppid() {
		return alipayAppid;
	}

	public void setAlipayAppid(String alipayAppid) {
		this.alipayAppid = alipayAppid;
	}

	@NotNull
	@Column(nullable = false, columnDefinition = "BLOB")
	@ColumnTransformer(read = "CAST(AES_DECRYPT(wx_mch_key, '" + Constant.DATABASE_AES_SECRET + "') as char(64))", write = "AES_ENCRYPT(?, '" + Constant.DATABASE_AES_SECRET + "')")
	public String getWxMchKey() {
		return wxMchKey;
	}

	public void setWxMchKey(String wxMchKey) {
		this.wxMchKey = wxMchKey;
	}

	@NotNull
	@Column(nullable = false)
	public Long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}

	@NotNull
	@Column(nullable = false, length = 16)
	public String getWxMchid() {
		return wxMchid;
	}

	public void setWxMchid(String wxMchid) {
		this.wxMchid = wxMchid;
	}

	@NotNull
	@Column(length = 16)
	public String getWxSubMchid() {
		return wxSubMchid;
	}

	public void setWxSubMchid(String wxSubMchid) {
		this.wxSubMchid = wxSubMchid;
	}

	@Column(length = 2048)
	public String getAlipayPriKey() {
		return alipayPriKey;
	}

	public void setAlipayPriKey(String alipayPriKey) {
		this.alipayPriKey = alipayPriKey;
	}

	@Column(length = 1024)
	public String getAlipayPubKey() {
		return alipayPubKey;
	}

	public void setAlipayPubKey(String alipayPubKey) {
		this.alipayPubKey = alipayPubKey;
	}

	@NotNull
	@Column(length = 30)
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	@NotNull
	@Column(length = 15)
	public String getForShort() {
		return forShort;
	}

	public void setForShort(String forShort) {
		this.forShort = forShort;
	}

	public String getMaxLogo() {
		return maxLogo;
	}

	public void setMaxLogo(String maxLogo) {
		this.maxLogo = maxLogo;
	}

	public String getMinLogo() {
		return minLogo;
	}

	public void setMinLogo(String minLogo) {
		this.minLogo = minLogo;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	@Transient
	public String getAppidByPlatform(Platform platform) {
		if (platform == Platform.weixin) {
			return getWxAppid();
		} else if (platform == Platform.alipay) {
			return getWxAppid();
		}
		return null;
	}

	@Transient
	public boolean isAlipayConfigOK() {
		return Pattern.compile("^\\d{16,32}$").matcher(this.alipayAppid).find() && !StringUtils.isEmpty(this.alipayPriKey) && !StringUtils.isEmpty(this.alipayPubKey);
	}
}
